package com.tbm.console.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.Controller2FTLConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.proj.entity.LocalLog;
import com.tbm.console.proj.entity.SystemImage;
import com.tbm.console.proj.service.LocalLogService;
import com.tbm.console.proj.service.SystemImagesService;
import com.tbm.console.util.DateUtil;
import com.tbm.console.util.JurisdictionUtil;
import com.tbm.console.util.ObjectExcelView;
import com.tbm.console.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class LocalLogController extends BaseController {

    @Autowired
    private SystemImagesService systemImagesService;

    @Autowired
    private LocalLogService localLogService;

    /**
     * 日志录入
     * @return
     */
    @RequestMapping("/log-entry-manage")
    public ModelAndView roleListManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "log/log-entry-manage";
            Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);
            List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
            modelAndView.addObject("buttons", buttonList);
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }


    /**
     * 通过UserID来获取所有的Images
     * @param id
     * @return
     */
    @RequestMapping("/log-images-show/{id}")
    public ModelAndView adminImages(@PathVariable("id") Integer id){
        PageData pageData = new PageData();
        pageData.put("userId", id);
        List<SystemImage> imagesEntityList = systemImagesService.findAll(pageData);
        ModelAndView mv = this.getModelAndView();
        mv.addObject("images", imagesEntityList);
        mv.setViewName("log/log-images-show");
        return mv;
    }


    @RequestMapping("/log-download")
    public ModelAndView adminDownload(){
        ModelAndView mv = this.getModelAndView();
        PageData pageData = this.getPageData();
        Object desc = pageData.get("desc");
        Object system = pageData.get("system");
        Object log = pageData.get("log");
        PageData pg = new PageData();
        if(null!=desc&&!"".equals(desc.toString())){
            pg.put("desc",desc);
        }
        if(null!=system&&!"".equals(system.toString())){
            pg.put("system",system);
        }
        if(null!=log&&!"".equals(log.toString())){
            pg.put("log", log);
        }
        pg.put("deleteFlag", InsUpdDelFlagConstant.LOCALLOGDELETEFLAG);
        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<String> titles = new ArrayList<String>();

        titles.add("系统名称"); 		    //1
        titles.add("合同分类");  		    //2
        titles.add("工作分类");			//3
        titles.add("参与人员");			//4
        titles.add("角色");			        //5
        titles.add("完成的工作内容");		//6

        titles.add("投入工作量");			//7
        titles.add("所属事业部");			//8
        titles.add("所属作业区");			//9
        titles.add("服务分类");			//10


        List<LocalLog> list = localLogService.findAll(pg);
        List<List<String>> varList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            List<String> varStrList = new ArrayList<>();
            varStrList.add(list.get(i).getLogTitle());
            varStrList.add(list.get(i).getLogContractType());
            varStrList.add(list.get(i).getLogJobType());
            varStrList.add(list.get(i).getLogCommitter());
            varStrList.add(list.get(i).getLogRole());
            varStrList.add(list.get(i).getLogContent());
            varStrList.add(String.valueOf(list.get(i).getLogWorkload()));
            varStrList.add(list.get(i).getBelongToBusinessUnit());
            varStrList.add(list.get(i).getBelongToDepartment());
            varStrList.add(list.get(i).getLogServiceType());
            varList.add(varStrList);
        }
        //Excel headerTitle
        dataMap.put("titles", titles);
        //Excel 内容
        dataMap.put("varList", varList);

        //Excel 文件名称
        dataMap.put("name", DateUtil.getID());

        //Excel Sheet

        ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
        mv = new ModelAndView(erv,dataMap);
        mv.addObject("result", "success");
        return mv;
    }
}
